package com.neuedu.web.service;

import com.neuedu.web.common.HisConstants;
import com.neuedu.web.common.PageInfo;
import com.neuedu.web.dao.*;
import com.neuedu.web.po.*;
import com.neuedu.web.utils.BeanConvertUtils;
import com.neuedu.web.utils.ParamUtils;
import com.neuedu.web.vo.DeptVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 金山
 * 项目：his-server
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2024/11/2813:52
 */
public class GuaHaoService {

    RegisterDao registerDao = new RegisterDao();
    UmsUserDao userDao = new UmsUserDao();

    private DeptDao deptDao = new DeptDao();
    private RegistLevelDao levelDao = new RegistLevelDao();
    private CheckApplyDao checkApplyDao = new CheckApplyDao();
    private InspectApplyDao inspectApplyDao = new InspectApplyDao();



    /**
     * 挂号
     * @param request
     * @param response
     * @return
     */
    public boolean guahao(HttpServletRequest request, HttpServletResponse response) {

        RegisterPO registerPO = ParamUtils.getParam(RegisterPO.class,request);

        //挂号待问诊
        registerPO.setStatus(HisConstants.REGISTER_STATUS_ED);
        int insert = registerDao.insert(registerPO);

        return insert>0;
    }

    /**
     * 初始化挂号页面  加载 部门列表 和挂号级别列表
     * @param request
     * @param response
     * @return
     * @throws NoSuchFieldException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public Map initGuahaoPage(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {

        Map map = new HashMap();

        map.put("deptList", deptDao.selectAll("select * from dept "));
        map.put("levelList", levelDao.selectAll(RegistLevelPO.class, "select * from regist_level"));

        return map;
    }

    /**
     * 挂号页面获取医生列表的
     * @param request
     * @param response
     * @return
     * @throws NoSuchFieldException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public List<UmsUser> getDoctor(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {


        String deptId = request.getParameter("deptId");
        String registLevel =  request.getParameter("registLevel");
        String sql = "select * from ums_user where dept_id = ?  and regist_level = ?";


        List<UmsUser> umsUsers = userDao.selectAll(sql, deptId, registLevel);
        return umsUsers;


    }


    public PageInfo<RegisterPO> page(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {


        String sql = " select * from register";


        PageInfo pageInfo = ParamUtils.getPageInfo(request);
        List<RegisterPO> dataList = registerDao.selectPage(pageInfo.getCurrent(), pageInfo.getSize(), sql);
        int total = registerDao.total(sql);

        pageInfo.setTotal(total);

        pageInfo.setData(dataList);


        // 数据
        return pageInfo;

    }

    public Map initFeePage(HttpServletRequest request, HttpServletResponse response) throws Exception{

        Map result = new HashMap();


        //患者id
        String registId = request.getParameter("registId");
        RegisterPO register = registerDao.selectById(Long.valueOf(registId));


        //根据患者查询已开立 检查项目
        List<CheckApplyPO> checkApplyList = checkApplyDao.selectAll(" SELECT   *  FROM  `check_apply`  WHERE `regist_id`  = ? ", registId);

        //根据患者查询已开立  检验项目
        List<InspectApplyPO> inspectApplyList = inspectApplyDao.selectAll(" SELECT   *  FROM  `inspect_apply`  WHERE `regist_id`  = ? ", registId);







        //患者信息
        result.put("register",register);
        result.put("checkApplyList",checkApplyList);
        result.put("inspectApplyList",inspectApplyList);

        return result;
    }

    public boolean fee(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {



        String strFeeItemIds= request.getParameter("feeItemId");
        String strFeeItemTypes = request.getParameter("feeItemType");

        String[] feeItemIds = strFeeItemIds.split(",");
        String[] feeItemTypes = strFeeItemTypes.split(",");

        for (int i = 0; i < feeItemIds.length; i++) {
            String feeItemId = feeItemIds[i];
            String feeItemType = feeItemTypes[i];
            if("check".equals(feeItemType)){ //检查申请
                CheckApplyPO applyPO = checkApplyDao.selectById(Long.valueOf(feeItemId));

                applyPO.setStatus(HisConstants.CHECK_APPLAY_STATUS_2);
                checkApplyDao.update(applyPO);

            }else if("inspect".equals(feeItemType)){
                InspectApplyPO applyPO = inspectApplyDao.selectById(Long.valueOf(feeItemId));

                applyPO.setStatus(HisConstants.INSPECT_APPLAY_STATUS_2);
                inspectApplyDao.update(applyPO);
            }
        }


        return true;

    }
}
